#include "..\\stdafx.h"
#include "EasyXPaint.h"
#include <graphics.h>	
#include<easyx.h>

void EasyXPaint::affectScaleFactor(FCRect *rect){
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
            rect->left = (int)(rect->left * m_scaleFactorX);
            rect->top = (int)(rect->top * m_scaleFactorY);
            rect->right = (int)(rect->right * m_scaleFactorX);
            rect->bottom = (int)(rect->bottom * m_scaleFactorY);
        }
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

EasyXPaint::EasyXPaint(){
	m_offsetX = 0;
	m_offsetY = 0;
	m_pRect.left = 0;
	m_pRect.top = 0;
	m_pRect.right = 0;
	m_pRect.bottom = 0;
	m_rotateAngle = 0;
	m_scaleFactorX = 0;
	m_scaleFactorY = 0;
	m_wRect.left = 0;
	m_wRect.top = 0;
	m_wRect.right = 0;
	m_wRect.bottom = 0;
	m_myColor = new MyColor;
	m_clip = 0;
}

EasyXPaint::~EasyXPaint(){
	if (m_clip) {
		DeleteObject(m_clip);
		m_clip = 0;
	}
	if(m_myColor){
		delete m_myColor;
		m_myColor = 0;
	}
	clearCaches();
}

void EasyXPaint::addArc(const FCRect& rect, float startAngle, float sweepAngle){
}

void EasyXPaint::addBezier(FCPoint *apt, int cpt){
}

void EasyXPaint::addCurve(FCPoint *apt, int cpt){
}

void EasyXPaint::addEllipse(const FCRect& rect){
}

void EasyXPaint::addLine(int x1, int y1, int x2, int y2){
}

void EasyXPaint::addRect(const FCRect& rect){
}

void EasyXPaint::addPie(const FCRect& rect, float startAngle, float sweepAngle){
}

void EasyXPaint::addText(String strText, FCFont *font, const FCRect& rect, int width){
}

void EasyXPaint::beginExport(const String& exportPath, const FCRect& rect){
}

void EasyXPaint::beginPaint(HDC hDC, const FCRect& wRect, const FCRect& pRect){
	m_pRect = pRect;
	m_wRect = wRect;
	int width = m_wRect.right - m_wRect.left;
	int height = m_wRect.bottom - m_wRect.top;
	m_resourcePath = L"";
	setbkcolor(WHITE);
	setbkmode(TRANSPARENT);
	//cleardevice();

}

void EasyXPaint::beginPath(){
}

void EasyXPaint::clearCaches(){
}

void EasyXPaint::clipPath(){
}

void EasyXPaint::closeFigure(){
}

void EasyXPaint::closePath(){
}

void EasyXPaint::drawArc(Long dwPenColor, float width, int style, const FCRect& rect, float startAngle, float sweepAngle){
}

void EasyXPaint::drawBezier(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
}

void EasyXPaint::drawCurve(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
}

void EasyXPaint::drawEllipse(Long dwPenColor, float width, int style, const FCRect& rect){
	drawEllipse(dwPenColor, width, style, rect.left, rect.top, rect.right, rect.bottom);
}

void EasyXPaint::drawEllipse(Long dwPenColor, float width, int style, int left, int top, int right, int bottom){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	FCRect newRect = { left + m_offsetX, top + m_offsetY, right + m_offsetX, bottom + m_offsetY };
	affectScaleFactor(&newRect);
	ellipse(newRect.left, newRect.top, newRect.right - 1, newRect.bottom - 1);
}

void EasyXPaint::drawImage(String imagePath, const FCRect& rect){
}

void EasyXPaint::drawLine(Long dwPenColor, float width, int style, const FCPoint& x, const FCPoint& y){
	drawLine(dwPenColor, width, style, x.x, x.y, y.x, y.y);
}

void EasyXPaint::drawLine(Long dwPenColor, float width, int style, int x1, int y1, int x2, int y2){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	int lx1 = x1 + m_offsetX;
	int ly1 = y1 + m_offsetY;
	int lx2 = x2 + m_offsetX;
	int ly2 = y2 + m_offsetY;
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1) {
		lx1 = (int)(m_scaleFactorX * lx1);
		ly1 = (int)(m_scaleFactorY * ly1);
		lx2 = (int)(m_scaleFactorX * lx2);
		ly2 = (int)(m_scaleFactorY * ly2);
	}
	line(lx1, ly1, lx2, ly2);
}

void EasyXPaint::drawPath(Long dwPenColor, float width, int style){
}

void EasyXPaint::drawPie(Long dwPenColor, float width, int style, const FCRect& rect, float startAngle, float sweepAngle){
}

void EasyXPaint::drawPolygon(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
	if (dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	int lx = 0, ly = 0;
	int fx = 0, fy = 0;
	for (int i = 0; i < cpt; i++) {
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1) {
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
		if (i > 0) {
			line(lx, ly, x, y);
			if (i == cpt - 1) {
				line(x, y, fx, fy);
			}
		}
		else {
			fx = x;
			fy = y;
		}
		lx = x;
		ly = y;
	}
}

void EasyXPaint::drawPolyline(Long dwPenColor, float width, int style, FCPoint *apt, int cpt){
if(cpt == 1){
		drawLine(dwPenColor, width, style, apt[0].x, apt[0].y, apt[0].x, apt[0].y);
		return;
	}
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	int lx = 0, ly = 0;
	for (int i = 0; i < cpt; i++) {
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1) {
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
		if (i > 0) {
			line(lx, ly, x, y);
		}
		lx = x;
		ly = y;
	}
}

void EasyXPaint::drawRect(Long dwPenColor, float width, int style, int left, int top, int right, int bottom){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	FCRect newRect = { left + m_offsetX, top + m_offsetY, right + m_offsetX, bottom + m_offsetY };
	affectScaleFactor(&newRect);
	rectangle(newRect.left, newRect.top, newRect.right - 1, newRect.bottom - 1);
}

void EasyXPaint::drawRect(Long dwPenColor, float width, int style, const FCRect& rect){
	drawRect(dwPenColor, width, style, rect.left, rect.top, rect.right, rect.bottom);
}

void EasyXPaint::drawRoundRect(Long dwPenColor, float width, int style, const FCRect& rect, int cornerRadius){
	if(dwPenColor == FCColor_None) return;
	drawRect(dwPenColor, 1, 0, rect);
}

void EasyXPaint::drawText(String strText, Long dwPenColor, FCFont *font, const FCRect& rect, int width){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	setfillcolor(dwPenColor);
	String fontFamily = font->m_fontFamily;
	if (fontFamily == L"Default") {
		fontFamily = L"Segoe UI";
	}
	float fontSize = font->m_fontSize + 6;
	double rate = 1;
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1) {
		rate = (m_scaleFactorX + m_scaleFactorY) / 2;
		fontSize = (float)(fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
	}
	settextstyle((int)(fontSize), 0, fontFamily.c_str());
	FCRect newRect = { rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY };
	affectScaleFactor(&newRect);
	outtextxy(newRect.left, newRect.top, strText.c_str());
}

void EasyXPaint::drawText(String strText, Long dwPenColor, FCFont *font, const FCRectF& rect, int width){
	FCRect rc = {(int)rect.left, (int)rect.top, (int)rect.right, (int)rect.bottom};
	drawText(strText, dwPenColor, font, rc, width);
}

void EasyXPaint::drawTextAutoEllipsis(String strText, Long dwPenColor, FCFont *font, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
	drawText(strText, dwPenColor, font, rect, -1);
}

void EasyXPaint::endExport(){
}

void EasyXPaint::endPaint(){
	int left = m_pRect.left;
	int top = m_pRect.top;
	int width = m_pRect.right - m_pRect.left;
	int height = m_pRect.bottom - m_pRect.top;
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1){
		left = (int)(m_scaleFactorX * left);
		top = (int)(m_scaleFactorY * top);
		width = (int)(m_scaleFactorX * width);
		height = (int)(m_scaleFactorY * height);
	}
	m_offsetX = 0;
	m_offsetY = 0;
	m_resourcePath = L"";
	FlushBatchDraw();
	if (m_clip) {
		DeleteObject(m_clip);
		m_clip = 0;
	}
}

void EasyXPaint::excludeClipPath(){
}

void EasyXPaint::fillEllipse(Long dwPenColor, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	setfillcolor(dwPenColor);
	FCRect newRect = { rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY };
	affectScaleFactor(&newRect);
	fillellipse(newRect.left, newRect.top, newRect.right, newRect.bottom);
}

void EasyXPaint::fillGradientEllipse(Long dwFirst, Long dwSecond, const FCRect& rect, int angle){
	fillEllipse(dwFirst, rect);
}

void EasyXPaint::fillGradientPath(Long dwFirst, Long dwSecond, const FCRect& rect, int angle){
}

void EasyXPaint::fillGradientPolygon(Long dwFirst, Long dwSecond, FCPoint *apt, int cpt, int angle){
	fillPolygon(dwFirst, apt, cpt);
}

void EasyXPaint::fillGradientRect(Long dwFirst, Long dwSecond, const FCRect& rect, int cornerRadius, int angle){
	if(cornerRadius != 0){
		fillRoundRect(dwFirst, rect, cornerRadius);
	}
	else{
		fillRect(dwFirst, rect);
	}
}

void EasyXPaint::fillPath(Long dwPenColor){
}

void EasyXPaint::fillPie(Long dwPenColor, const FCRect& rect, float startAngle, float sweepAngle){
}

void EasyXPaint::fillPolygon(Long dwPenColor, FCPoint *apt, int cpt){
	if (dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	setfillcolor(dwPenColor);
	for (int i = 0; i < cpt; i++) {
		int x = apt[i].x + m_offsetX;
		int y = apt[i].y + m_offsetY;
		if (m_scaleFactorX != 1 || m_scaleFactorY != 1) {
			x = (int)(m_scaleFactorX * x);
			y = (int)(m_scaleFactorY * y);
		}
		apt[i].x = x;
		apt[i].y = y;
	}
	fillpolygon(apt, cpt);
}

void EasyXPaint::fillRect(Long dwPenColor, const FCRect& rect){
	if(dwPenColor == FCColor_None) return;
	dwPenColor = getPaintColor(dwPenColor);
	if (dwPenColor < 0) dwPenColor = -dwPenColor / 1000;
	setcolor(dwPenColor);
	setfillcolor(dwPenColor);
	FCRect newRect = { rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY };
	affectScaleFactor(&newRect);
	fillrectangle(newRect.left, newRect.top, newRect.right, newRect.bottom);
}

void EasyXPaint::fillRect(Long dwPenColor, int left, int top, int right, int bottom){
	FCRect newRect = {left, top, right, bottom};
	fillRect(dwPenColor, newRect);
}

void EasyXPaint::fillRoundRect(Long dwPenColor, const FCRect& rect, int cornerRadius){
	if(dwPenColor == FCColor_None) return;
	fillRect(dwPenColor, rect);
}

Long EasyXPaint::getColor(Long dwPenColor){
	return m_myColor->getUserColor(dwPenColor);
}

Long EasyXPaint::getPaintColor(Long dwPenColor){
	return getColor(dwPenColor);
}

FCPoint EasyXPaint::getOffset(){
	FCPoint offset = {m_offsetX, m_offsetY};
	return offset;
}

void EasyXPaint::getScaleFactor(double *scaleFactorX, double *scaleFactorY){
	*scaleFactorX = m_scaleFactorX;
	*scaleFactorY = m_scaleFactorY;
}

FCPoint EasyXPaint::rotate(const FCPoint& op, const FCPoint& mp, int angle){
	FCPoint pt = {0};
	return pt;
}

void EasyXPaint::setClip(const FCRect& rect){
	if (m_clip) {
		DeleteObject(m_clip);
		m_clip = 0;
	}
	setcliprgn(NULL);
	FCRect newRect = { rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY };
	affectScaleFactor(&newRect);
	m_clip = CreateRectRgn(newRect.left, newRect.top, newRect.right, newRect.bottom);
	setcliprgn(m_clip);
}

void EasyXPaint::setLineCap(int startLineCap, int endLineCap){
}

void EasyXPaint::setOffset(const FCPoint& offset){
	m_offsetX = offset.x;
	m_offsetY = offset.y;
}

void EasyXPaint::setOpacity(float opacity){
}

void EasyXPaint::setResourcePath(const String& resourcePath){
	m_resourcePath = resourcePath;
}

void EasyXPaint::setRotateAngle(int rotateAngle){
	m_rotateAngle = rotateAngle;
}

void EasyXPaint::setScaleFactor(double scaleFactorX, double scaleFactorY){
	m_scaleFactorX = scaleFactorX;
	m_scaleFactorY = scaleFactorY;
}

bool EasyXPaint::supportTransparent(){
	return false;
}

FCSize EasyXPaint::textSize(String strText, FCFont *font, int width){
	String fontFamily = font->m_fontFamily;
	if (fontFamily == L"Default") {
		fontFamily = L"Segoe UI";
	}
	float fontSize = font->m_fontSize + 6;
	double rate = 1;
	if (m_scaleFactorX != 1 || m_scaleFactorY != 1) {
		rate = (m_scaleFactorX + m_scaleFactorY) / 2;
		fontSize = (float)(fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
	}
	settextstyle((int)(fontSize), 0, fontFamily.c_str());
	int w = textwidth(strText.c_str());
	int h = textheight(strText.c_str());
	FCSize size = { w / rate, h / rate };
	return size;
}

FCSizeF EasyXPaint::textSizeF(String strText, FCFont *font, int width){
	FCSize size = textSize(strText, font, width);
	FCSizeF fSize = {(float)size.cx, (float)size.cy};
	return fSize;
}